package com.chaihu.front.service.menu.impl;

import com.alibaba.fastjson.JSONObject;
import com.chaihu.core.base.service.front.BaseComponentService;
import com.chaihu.core.context.IPageData;
import com.chaihu.front.service.menu.MenuService;
import com.chaihu.utils.constant.ServiceCodeConstant;
import com.chaihu.utils.constant.ServiceCodeMenuConstant;
import com.chaihu.utils.constant.ServiceConstant;
import com.chaihu.utils.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 登录信息实现类
 * Created by wuxw on 2019/3/20.
 */

@Slf4j
@Service("menuServiceImpl")
public class MenuServiceImpl extends BaseComponentService implements MenuService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public ResponseEntity<String> getMenusByUserId(IPageData pd) {
        Assert.hasLength(pd.getUserId(), "用户还没有登录");

        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeConstant.SERVICE_CODE_ADMIN_QUERY_MENU_INFO + "?userId=" + pd.getUserId();
        return this.callCenterService(restTemplate, pd, "", apiUrl, HttpMethod.GET);
    }

    @Override
    public ResponseEntity<String> listMenus(IPageData pd) {
        Map paramIn = (Map) JSONObject.parse(pd.getReqData());
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeMenuConstant.LIST_MENUS + mapToUrlParam(paramIn);
        return this.callCenterService(restTemplate, pd, "", apiUrl, HttpMethod.GET);
    }

    @Override
    public ResponseEntity<String> saveMenu(IPageData pd) {
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeMenuConstant.ADD_MENU;
        return this.callCenterService(restTemplate, pd, pd.getReqData(), apiUrl, HttpMethod.POST);
    }

    @Override
    public ResponseEntity<String> updateMenu(IPageData pd) {
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeMenuConstant.UPDATE_MENU;
        return this.callCenterService(restTemplate, pd, pd.getReqData(), apiUrl, HttpMethod.POST);
    }

    @Override
    public ResponseEntity<String> deleteMenu(IPageData pd) {
        String apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeMenuConstant.DELETE_MENU;
        return this.callCenterService(restTemplate, pd, pd.getReqData(), apiUrl, HttpMethod.POST);
    }
}
